﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormsApp1
{
    public partial class StatusBarTransparent : Form
    {
        [DllImport("dwmapi.dll")]
        private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);

        private const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
        private const int DWMWA_MICA_EFFECT = 1029;
        private const int DWMWA_SYSTEMBACKDROP_TYPE = 38;


        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);


      

        public StatusBarTransparent()
        {
            InitializeComponent();
         //   ApplyTransparency(1);
            // 获取任务栏句柄
            IntPtr taskbarHandle = FindWindow("Shell_TrayWnd", null);

            if (taskbarHandle != IntPtr.Zero)
            {
                // 启用透明效果
                int enable = 1; // 1 启用，0 禁用

                // 尝试设置 Mica 效果（Win11）
                DwmSetWindowAttribute(taskbarHandle, DWMWA_MICA_EFFECT, ref enable, sizeof(int));

                // 或者尝试设置系统背景类型
                int backdropType = 3; // 3 对应 Acrylic/Mica 效果
                DwmSetWindowAttribute(taskbarHandle, DWMWA_SYSTEMBACKDROP_TYPE, ref backdropType, sizeof(int));
            }
        }
        [DllImport("dwmapi.dll")]
        private static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMargins);

        private struct MARGINS
        {
            public int Left;
            public int Right;
            public int Top;
            public int Bottom;
        }

        public static void ApplyTransparency(IntPtr hWnd)
        {
            MARGINS margins = new MARGINS { Left = -1, Right = -1, Top = -1, Bottom = -1 };
            DwmExtendFrameIntoClientArea(hWnd, ref margins);
        }
        private void StatusBarTransparent_Load(object sender, EventArgs e)
        {

        }
    }
}
